# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import os
from common import *
from openCv import *

class scPveFightIng(myScene):
    def __init__(self,bValid):
        self.bValid=bValid
        self.enemyPos=[0,0]
        self.selfPos=[0,0]
        self.offset1=[-480,-320]
        self.offset2=[-30,-190]
        self.offset3=[-480,-20]
        self.bLastSkill=False
        self.lastSkill=None
        self.endPos=[0,0]
        self.midPosY=0
        self.name='scPveFightIng'
        self.path='resource/mercenary/pve/scPveFightIng'
        self.tagPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('tag')]
        self.funcPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('func')]
        self.funcAoePng=[myPng(self.path,png) \
            for png in os.listdir(self.path) if png.startswith('funcAoe')]
        self.funcWaitPng=[myPng(self.path,png) \
            for png in os.listdir(self.path) if png.startswith('funcWait')]
        self.funcMustPng=[myPng(self.path,png) \
            for png in os.listdir(self.path) if png.startswith('funcMust')]
        self.funcSwitchPng=[myPng(self.path,png) \
            for png in os.listdir(self.path) if png.startswith('funcSwitch')]

    def enable(self):
        self.bValid=True

    def dis(self):
        self.bValid=False

    def isOwn(self,background):
        if self.bValid:
            tagList={}
            for tag in self.tagPng:
                bFind,x,y,w,h=bFindInBackground(background,tag,0.80)
                if bFind:
                    tagList[tag.name]=(x,y,w,h)
            if ('tagSkull1' in tagList):
                pos=tagList['tagSkull1']
                self.enemyPos[0]=pos[0]+self.offset1[0]
                self.enemyPos[1]=pos[1]+self.offset1[1]
                self.selfPos[0]=pos[0]+self.offset3[0]
                self.selfPos[1]=pos[1]+self.offset3[1]
                self.endPos[0]=pos[0]+self.offset2[0]
                self.endPos[1]=pos[1]+self.offset2[1]
                self.midPosY=pos[1]-100
                return True
            elif ('tagSkull2' in tagList):
                return True
            else:
                return False
        else:
            return False

    def proc(self,background):

        #循环释放技能
        bNeedSave=False
        bg=background
        runTime=time.time()
        while True:

            #重新截图判断
            if bNeedSave:
                bg=SaveScreen()
                if not self.isOwn(bg):
                    return False

            #挂机中
            if MyGui.gAbandon==999 and MyGui.gHookTime>0:
                MyGui.gHookTime=MyGui.G_HOOK_TIME+random.randint(60,180)
                MyGui.gWait=MyGui.gHookTime
                time.sleep(MyGui.gHookTime)
                MyGui.gHookTime=0

            #技能释放超时(对面有隐身等)，直接点结束
            if ((time.time()-runTime)>30):
                Click(self.endPos[0],self.endPos[1],b='right')
                moveAndClick(self.endPos[0],self.endPos[1],10) 
                return True

            #准备自动上英雄
            for func in self.funcPng:
                if (func.name=='funcHeroNone1') or \
                    (func.name=='funcHeroNone2') or \
                    (func.name=='funcHeroNone3'):
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.75)
                    if bFind:
                        moveAndClick(x+w/2,y+h/2,12)
                        return True

            #是否技能全部释放完毕
            for func in self.funcPng:
                if (func.name=='funcSkillOk'):
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                    if bFind:
                        self.bLastSkill=False
                        moveAndClick(x+w/2,y+h/2,10) 
                        return True

            #aoe技能
            bNeedAoe=False
            bFindAoe=False
            if (MyGui.gSkill=='1') or (MyGui.gSkill=='2') or \
                (MyGui.gSkill=='3') or (MyGui.gSkill=='4'):
                bNeedAoe=True
            
            #寻找第一轮就能释放的AOE技能，可以都提前固定好
            if bNeedAoe:
                for func in self.funcAoePng:
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                    if bFind:
                        bFindAoe=True
                        moveAndClick(x+w/2,y+h/2,0.5)
                        break
            if bFindAoe:
                bNeedSave=True
                continue

            #寻找等待一回合的AOE技能，可以都提前固定好
            if bNeedAoe:
                for func in self.funcWaitPng:
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                    if bFind:
                        bFindAoe=True
                        moveAndClick(x+w/2,y+h/2,0.5)
                        break
            if bFindAoe:
                bNeedSave=True
                continue

            #必须释放技能（自然队等）
            bNeedMust=False
            bFindMust=False
            if (MyGui.gSkill=='5'):
                bNeedMust=True

            #寻找必须释放技能（自然队等）
            if bNeedMust:
                for func in self.funcMustPng:
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                    if bFind:
                        bFindMust=True
                        Drag(x+w/2,y+h/2,self.enemyPos[0],self.enemyPos[1],0.5)
                        break
            if bFindMust:
                bNeedSave=True
                continue

            #寻找未释放技能英雄（多图匹配）
            func=None
            for f in self.funcPng:
                if f.name=='funcSkillNo':
                    func=f
                    break
            bFind,okList=bFindMultInBackground(bg,func,0.70)
            if bFind:
                validPos=[]
                for pos in okList:
                    if pos[1]>self.midPosY:
                        validPos.append(pos)
                x=0
                y=0
                diffHero=[-30,60]
                validPos.sort()
                if len(validPos)>1:
                    pos=validPos[random.randint(0,len(validPos)-1)]
                    x=pos[0]+pos[2]/2
                    y=pos[1]+pos[3]/2
                elif len(validPos)==1:
                    pos=validPos[0]
                    diffX=[-115,0,115]
                    diff=diffX[random.randint(0,2)]
                    x=pos[0]+pos[2]/2+diff
                    y=pos[1]+pos[3]/2
                if x>0:
                    moveAndClick(x+diffHero[0],y+diffHero[1])
                    Click(x+diffHero[0],y+diffHero[1],b='right')
                self.bLastSkill=False
                bNeedSave=True
                continue

            '''
            #技能类型：
            1.拖拽打对面
            2.2选1，打对面，给自己buff
            3.抉择，单击
            4.抉择，拖拽
            是否抉择，单击或拖拽，对面或友方或自己或2选择1
            '''

            #寻找可释放技能（多图匹配）
            for f in self.funcPng:
                if f.name=='funcSkillCom':
                    func=f
                    break
            bFind,okList=bFindMultInBackground(bg,func,0.70)
            if bFind:

                #释放新技能
                bSkill=False
                skillPngSize=[46,46]
                if not self.bLastSkill:
                    bSkill=True
                else:
                    #判断上次技能是否拖动成功
                    bFind,x,y,w,h=bFindInBackground(bg,self.lastSkill,0.90)
                    if bFind:
                        #拖动失败，说明为非指向性技能，需要单击
                        moveAndClick(x+skillPngSize[0]/2,y+skillPngSize[1]/2,0.5)

                        #判断是否为抉择型技能
                        bg=SaveScreen()
                        for f in self.funcPng:
                            if f.name=='funcSkillInsure':
                                func=f
                                break
                        bFind,okList=bFindMultInBackground(bg,func,0.80)
                        if bFind:
                            diffInsure=[0,240]
                            okList.sort()
                            pos=okList[random.randint(0,len(okList)-1)]
                            x=pos[0]+pos[2]/2+diffInsure[0]
                            y=pos[1]+pos[3]/2+diffInsure[1]
                            moveAndClick(x,y,0.5)
                            bg=SaveScreen()
                            bFind,x,y,w,h=bFindInBackground(bg,self.lastSkill,0.90)
                            if bFind:
                                moveAndClick(self.enemyPos[0],self.enemyPos[1],0.5)
                        self.bLastSkill=False
                    else:
                        #拖动成功，需要释放技能
                        bSkill=True

                #释放技能
                if bSkill:
                    skill1=[False,[]]
                    skill2=[False,[]]
                    skill3=[False,[]]
                    diffCom=130
                    okList.sort()

                    #判断
                    skill1[0]=True
                    skill1[1]=okList[0]
                    if len(okList)>=2:
                        if (okList[1][0]-okList[0][0])<(diffCom*1.5):
                            skill2[0]=True
                            skill2[1]=okList[1]
                            if len(okList)>=3:
                                if (okList[2][0]-okList[0][0])>(diffCom*1.5) and \
                                    (okList[2][0]-okList[0][0])<(diffCom*2.5):
                                    skill3[0]=True
                                    skill3[1]=okList[2]
                        elif (okList[1][0]-okList[0][0])>(diffCom*1.5) and \
                                (okList[1][0]-okList[0][0])<(diffCom*2.5):
                            skill3[0]=True
                            skill3[1]=okList[1]

                    #选择
                    pos=skill1[1]
                    if (MyGui.gSkill=='2' or MyGui.gSkill=='6') and skill2[0]:
                        pos=skill2[1]
                    elif (MyGui.gSkill=='3' or MyGui.gSkill=='7') and skill3[0]:
                        pos=skill3[1]
                    elif MyGui.gSkill=='4' or MyGui.gSkill=='8':
                        all=[]
                        if skill1[0]:all.append(skill1[1])
                        if skill2[0]:all.append(skill2[1])
                        if skill3[0]:all.append(skill3[1])
                        pos=all[random.randint(0,len(all)-1)]

                    #释放
                    diffHero=[-15,-50]
                    diffSkill=[-25,-20]
                    x=pos[0]+pos[2]/2+diffHero[0]
                    y=pos[1]+pos[3]/2+diffHero[1]
                    self.bLastSkill=True
                    self.lastSkill=SaveLastSkillPng(x+diffSkill[0],\
                        y+diffSkill[1],skillPngSize[0],skillPngSize[1])
                    bSelf=False
                    #判断是否为2选1技能（既打对面和给自己buff）
                    for func in self.funcSwitchPng:
                        bFind1,x1,y1,w1,h1=bFindInBackground(self.lastSkill.data,func,0.90)
                        if bFind1:
                            bSelf=False if(random.randint(0,1)==0) else True
                            break
                    if bSelf:
                        Drag(x,y,self.selfPos[0],self.selfPos[1],0.5)
                    else:
                        Drag(x,y,self.enemyPos[0],self.enemyPos[1],0.5)
                    bNeedSave=True
                    continue
            bNeedSave=True
